﻿Shader "Unlit/FontUnlitShader"
{
    Properties
    {
        xPixels("xPixels", 2D) = "white" {}
        xPalette("xPalette", 2D) = "white" {}
    }
        SubShader
    {
        Tags { "RenderType" = "Transparent" }
        // 透明的Shader
        ZWrite Off // 关闭深度写入
        Blend SrcAlpha OneMinusSrcAlpha // 混合的参数
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

         #define DECLARE_TEXTURE(Name) \
             sampler2D Name

         #define SAMPLE_TEXTURE(Name, texCoord)  tex2D(Name, texCoord)



            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float4 Color: COLOR0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float4 Color: COLOR0;
            };

            sampler2D xPixels;
            sampler2D xPalette;
            float4 xPixels_ST;

            float4x4 xMatrix;
            float4x4 xRotation;

            float xBlendWeight;

            int xFontColorIndex;
            int xFontTotalColors;

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = mul(mul(v.vertex, xMatrix), xRotation);
                o.uv = v.uv;
                o.Color = v.Color;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                //    // sample the texture
                //   fixed4 col = tex2D(xPixels, i.uv);
                //// apply fog
                //UNITY_APPLY_FOG(i.fogCoord, col);
                //return col;
                    float color_index = SAMPLE_TEXTURE(xPixels, i.uv).r;
                    if (color_index == 0.0f) return float4(0, 0, 0, 0);

                    float per = 1.0 - float(xFontColorIndex) / float(xFontTotalColors);
                    float4 output_color = SAMPLE_TEXTURE(xPalette, color_index * per - 1.0 / 255.0);

                    output_color *= i.Color;

                    return float4(output_color.bgr, xBlendWeight);
                }
           ENDCG
            }
    }
}
